App.define("yDate", function () {
    function yDate(year, month, day) {
        this._year = year;
        this._month = month;
        this._day = day;
    }
    yDate.prototype = {
        constructor: yDate,
        get year() {
            return this._year;
        },
        get month() {
            return this._month;
        },
        get day() {
            return this._day;
        },
        set year(value) {
            value = parseInt(value);
            this._year = value;
        },
        set month(value) {
            value = parseInt(value);
            if (value <= 0) this._month = 1;
            else if (value > 12) this._month = 12;
            else this._month = value;
        },
        set day(value) {
            value = parseInt(value);
            var days = yDate.getMonthDay(this._year, this._month);
            if (value <= 0) this._day = 1;
            else if (value > days) this._day = days;
            else this._day = value;
        },
        toString: function () {
            if (!this._year) return "";
            return this._year + "年" + this._month + "月" + this._day + "日";
        },
        toFormatString: function (splitStr) {
            splitStr = splitStr || "-";
            return [
                this._year,
                this._wrap(this._month),
                this._wrap(this._day)
            ].join(splitStr);
        },
        _wrap: function (num) {
            return ("0" + num).slice(-2);
        },
        toYearMonth: function () {
            return this._year + "年" + this._month + "月";
        },
        toMonthDay: function () {
            return this._month + "月" + this._day + "日";
        },
        compare: function (year, month, day) {
            if (year.constructor === yDate) {
                month = year._month;
                day = year._day;
                year = year._year;
            }
            if (this._year > year) return 1;
            if (this._year === year && this._month > month) return 1;
            if (this._year === year && this._month === month && this._day > day) return 1;
            if (this._year === year && this._month === month && this._day === day) return 0;
            return -1;
        },
        previousDay: function () {
            if (this._day === 1) {
                this.previousMonth();
                this._day = yDate.getMonthDay(this._year, this._month);
            } else {
                this._day -= 1;
            }
            return this;
        },
        nextDay: function () {
            var days = yDate.getMonthDay(this._year, this._month);
            if (days === this._day) {
                this.nextMonth();
                this._day = 1;
            } else {
                this._day += 1;
            }
            return this;
        },
        nextMonth: function () {
            if (this._month == 12) {
                this._month = 1;
                this._year++;
            } else {
                this._month++;
            }
            return this;
        },
        previousMonth: function () {
            if (this._month == 1) {
                this._month = 12;
                this._year--;
            } else {
                this._month--;
            }
            return this;
        },
        nextYear: function () {
            this._year++;
            return this;
        },
        previousYear: function () {
            this._year--;
            return this;
        },
        getActiveDay: function () {
            var weekday = yDate.getWeekDay(this._year, this._month, 1);
            weekday = weekday || 7;
            return weekday + this._day - 1;
        },
        clone: function (date) {
            date = date || new yDate();
            date.year = this._year;
            date.month = this._month;
            date.day = this._day;
            return date;
        },
        getMonthDays: function () {
            return {
                prevNum: yDate.getWeekDay(this._year, this._month, 1),
                num: yDate.getMonthDay(this._year, this._month)
            }
        },
        set: function (year, month, day) {
            this._year = year;
            this._month = month;
            this._day = day;
            return this;
        }
    };
    yDate.isLeapYear = function (year) {
        year = year || 0;
        if (year % 4 !== 0 || (year % 100 === 0 && year % 400 !== 0)) {
            return false;
        }
        return true;
    };
    yDate.getMonthDay = function (year, month) {
        var isLeapYear = yDate.isLeapYear(year);
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (isLeapYear) {
                    return 29;
                } else {
                    return 28;
                }
                default:
                    alert('error month');
                    break;
        }
    };
    yDate.getWeekDay = function (year, month, day) {
        return (new Date(year, month - 1, day)).getDay();
    };
    yDate.fromNow = function () {
        var now = new Date();
        return new yDate(now.getFullYear(), now.getMonth() + 1, now.getDate());
    };
    yDate.getNextData = function (value) {
        var data = [];
        for (var i = 0; i < 60; i++) {
            data.push({
                value: i,
                content: ("0" + i).slice(-2)
            })
        }
        return data;
    };
    yDate.getOriginData = function (value) {
        var first = {
                index: 0,
                items: []
            },
            second = {
                index: 0,
                items: []
            };
        for (var i = 0; i < 24; i++) {
            if (parseInt(value[0]) == i) first.index = i + 1;
            first.items.push({
                value: i,
                content: ("0" + i).slice(-2)
            })
        }
        for (var i = 0; i < 60; i++) {
            if (parseInt(value[1]) == i) second.index = i + 1;
            second.items.push({
                value: i,
                content: ("0" + i).slice(-2)
            })
        }
        return [first, second];
    }
    return yDate;
})